package com.yellowpage.ca.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yellowpage.ca.data.FuncModule;
import com.yellowpage.ca.data.Function;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.FuncModuleService;
import com.yellowpage.ca.service.FunctionService;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;

public class FunctionAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	private List<Function> functionList = new ArrayList<Function>();
	private List<Map> resultList = new ArrayList<Map>();

	private Function function;
	private FunctionService functionService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_functionId;
	private String query_funcModuleId;	
	private String query_funcCode;
	private String query_name;
	private String query_path;
	private String query_status;
	

	private FuncModuleService funcModuleService;
	private List<FuncModule> funcModuleList = new ArrayList<FuncModule>();
	
	public List<FuncModule> getFuncModuleList() {
		return funcModuleList;
	}

	public void setFuncModuleList(List<FuncModule> funcModuleList) {
		this.funcModuleList = funcModuleList;
	}

	public void prepare() {
		super.prepare();
		
		funcModuleList = funcModuleService.getAllFuncModule();
	
		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.function = functionService.getFunction(Long.parseLong(selectedId));
		}
	}

	public String query() {
		Map queryMap = new HashMap();

		initPageRender(10);
		functionList = functionService.queryFunction(queryMap, pageRender);

		return RESULT_QUERY;
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("functionId", query_functionId);
		queryMap.put("funcModuleId", query_funcModuleId);		
		queryMap.put("funcCode", query_funcCode);
		queryMap.put("name", query_name);
		queryMap.put("path", query_path);
		queryMap.put("status", query_status);

		resultList = functionService.queryFunctionByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {
		function.setCreateTime(new Date());
		function.setCreateUser(loginUser.getUserId());	
		function.setLastUpdateTime(new Date());
		function.setLastUpdateUser(loginUser.getUserId());	
		function.setStatus("1");

		functionService.saveFunction(function);

		message = "新增成功!";
		return RESULT_ADD;
	}
	
	public void validateSave() {			
		if (function.getName().length() == 0) {
			this.addActionError("====== 功能点名称 不能为空 ======");
		}
		if (function.getFuncCode().length() == 0) {
			this.addActionError("====== 功能点代码 不能为空 ======");
		}
		if (function.getPath().length() == 0) {
			this.addActionError("====== 功能点路径 不能为空 ======");
		}	
		this.setValidatorResult("/ca/FunctionAdd.jsp");		
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		function = functionService.getFunction(id);

		return RESULT_EDIT;
	}

	public String update() {
		function.setLastUpdateTime(new Date());
		function.setLastUpdateUser(loginUser.getUserId());	

		functionService.updateFunction(function);

		message = "修改成功!";

		return RESULT_EDIT;
	}
	
	public void validateUpdate() {		
		if (function.getName().length() == 0) {
			this.addActionError("====== 功能点名称 不能为空 ======");
		}
		if (function.getFuncCode().length() == 0) {
			this.addActionError("====== 功能点代码 不能为空 ======");
		}
		if (function.getPath().length() == 0) {
			this.addActionError("====== 功能点路径 不能为空 ======");
		}			
		this.setValidatorResult("/ca/FunctionUpdate.jsp");	
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		this.functionService.delete(ids);

		return query();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setFunction(Function function) {
		this.function = function;
	}

	public Function getFunction() {
		return function;
	}

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	public List<Function> getFunctionList() {
		return functionList;
	}

	public void setFunctionList(List<Function> functionList) {
		this.functionList = functionList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_functionId() {
		return this.query_functionId;
	}

	public void setQuery_functionId(String functionId) {
		this.query_functionId = functionId;
	}

	public String getQuery_funcModuleId() {
		return this.query_funcModuleId;
	}

	public void setQuery_funcModuleId(String funcModuleId) {
		this.query_funcModuleId = funcModuleId;
	}	

	public String getQuery_funcCode() {
		return this.query_funcCode;
	}

	public void setQuery_funcCode(String funcCode) {
		this.query_funcCode = funcCode;
	}

	public String getQuery_name() {
		return this.query_name;
	}

	public void setQuery_name(String name) {
		this.query_name = name;
	}

	public String getQuery_path() {
		return this.query_path;
	}

	public void setQuery_path(String path) {
		this.query_path = path;
	}

	public String getQuery_status() {
		return this.query_status;
	}

	public void setQuery_status(String status) {
		this.query_status = status;
	}
	
	public FuncModuleService getFuncModuleService() {
		return funcModuleService;
	}

	public void setFuncModuleService(FuncModuleService funcModuleService) {
		this.funcModuleService = funcModuleService;
	}

}
